package com.zygh.policeasset.service;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zygh.policeasset.component.CommonResult;
import com.zygh.policeasset.mapper.AuditFlowMapper;
import com.zygh.policeasset.mapper.DailyProjectMapper;
import com.zygh.policeasset.mapper.PurchaseMapper;
import com.zygh.policeasset.mapper.PurchaseQrCodeInfoMapper;
import com.zygh.policeasset.mapper.QrCodeGeneratedMapper;
import com.zygh.policeasset.mapper.QrCodeInfoMapper;
import com.zygh.policeasset.mapper.SettlementMapper;
import com.zygh.policeasset.pojo.QrCodeGenerated;
import com.zygh.policeasset.pojo.QrCodeInfo;
import com.zygh.policeasset.pojo.TableType;
import com.zygh.policeasset.pojo.dto.AppQrCodeDistributionDTO;
import com.zygh.policeasset.pojo.dto.QrCodeDistributionDTO;
import com.zygh.policeasset.pojo.imports.QrCodeInfoImport;
import com.zygh.policeasset.pojo.vo.ProjectInfoVO;
import com.zygh.policeasset.pojo.vo.QrCodeGeneratedVO;
import com.zygh.policeasset.utils.AuditUtil;
import com.zygh.policeasset.utils.EasyExcelUtil;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

@Service
/* loaded from: police-asset-2.0-0.0.1-SNAPSHOT.jar:BOOT-INF/classes/com/zygh/policeasset/service/QrCodeGeneratedService.class */
public class QrCodeGeneratedService {
    @Autowired
    QrCodeGeneratedMapper qrCodeGeneratedMapper;
    @Autowired
    SettlementMapper settlementMapper;
    @Autowired
    PurchaseMapper purchaseMapper;
    @Autowired
    DailyProjectMapper dailyProjectMapper;
    @Autowired
    EasyExcelUtil easyExcelUtil;
    @Autowired
    PurchaseQrCodeInfoMapper purchaseQrCodeInfoMapper;
    @Autowired
    QrCodeInfoMapper qrCodeInfoMapper;
    @Autowired
    AuditFlowMapper auditFlowMapper;
    @Autowired
    AuditFlowService auditFlowService;

    public Integer add(QrCodeGenerated qrCodeGenerated) {
        String now = DateUtil.now();
        String flowNo = AuditUtil.getFlowNo(now);
        qrCodeGenerated.setFlowNo(flowNo);
        qrCodeGenerated.setCreateTime(now);
        this.auditFlowService.add(qrCodeGenerated.getUserId(), flowNo, now, TableType.QRCODEGENERATED.getName(), TableType.QRCODEGENERATED.getType(), qrCodeGenerated.getBuildNum(), qrCodeGenerated.getSectionId());
        return this.qrCodeGeneratedMapper.add(qrCodeGenerated);
    }

    @Transactional(rollbackFor = {Exception.class})
    public Integer delete(Integer id) {
        try {
            this.qrCodeInfoMapper.deleteByQrCodeGeneratedId(id, 2);
            return this.qrCodeGeneratedMapper.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return 0;
        }
    }

    public Integer deleteByQrCodeGeneratedId(Integer qrCodeGeneratedId) {
        try {
            return this.qrCodeGeneratedMapper.delete(qrCodeGeneratedId);
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return 0;
        }
    }

    public Integer printQrCodeDelete(String ids) {
        Integer count = null;
        for (String id : ids.split(",")) {
            count = this.qrCodeInfoMapper.printQrCodeDelete(Integer.valueOf(Integer.parseInt(id)));
        }
        return count;
    }

    public List<ProjectInfoVO> selectProject(String type, String name, String buildNum, Integer sectionId, Integer unitId) {
        List<ProjectInfoVO> list = new ArrayList();
        if ("jc".equals(type)) {
            list = this.settlementMapper.getProjectInfo(name, buildNum, sectionId, 2, unitId);
        } else if ("zn".equals(type)) {
            list = this.settlementMapper.getProjectInfo(name, buildNum, sectionId, 1, unitId);
        } else if ("da".equals(type)) {
            list = this.dailyProjectMapper.getProjectInfo(name, buildNum, unitId);
        } else if ("zd".equals(type)) {
            list = this.purchaseMapper.getProjectInfo(name, unitId);
        }
        return list;
    }

    public Object selectQrCode(Integer qrCodeGeneratedId, HttpServletResponse response) {
        List<QrCodeInfo> list = this.qrCodeInfoMapper.selectListByQrCodeGeneratedId(qrCodeGeneratedId, 1);
        if (list.size() > 0) {
            return this.easyExcelUtil.exportQrCode(response, list);
        }
        return new CommonResult().validateFailed("无数据");
    }

    public String selectQrCodePrintQrCode(Integer qrCodeGeneratedId, HttpServletResponse response) {
        List<QrCodeInfoImport> list = this.qrCodeInfoMapper.findPrintQrCode(qrCodeGeneratedId, 2);
        if (list.size() > 0) {
            return this.easyExcelUtil.exportQrCodePrintQrCode(response, list);
        }
        return "";
    }

    public PageInfo<QrCodeGeneratedVO> selectList(int pageNum, int pageSize, String type, String name, String buildNum, Integer sectionId, Integer companyId, String qrCode, Integer userId, Integer status) {
        PageInfo<QrCodeGeneratedVO> source = PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> {
            List<QrCodeGeneratedVO> list = this.qrCodeGeneratedMapper.selectList(type, name, buildNum, sectionId, companyId, qrCode, userId, status);
        });
        PageInfo<QrCodeGeneratedVO> target = new PageInfo();
        BeanUtils.copyProperties(source, target);
        List<QrCodeGeneratedVO> list = tolist(source.getList());
        target.setList(list);
        return target;
    }

    public QrCodeGeneratedVO selectById(Integer id) {
        return toObject(this.qrCodeGeneratedMapper.selectById(id));
    }

    public QrCodeGeneratedVO toObject(QrCodeGeneratedVO qrCodeGenerated) {
        if ("jc".equals(qrCodeGenerated.getType())) {
            qrCodeGenerated.setTypeName("基础");
        } else if ("znh".equals(qrCodeGenerated.getType())) {
            qrCodeGenerated.setTypeName("智能硬件");
        } else if ("zns".equals(qrCodeGenerated.getType())) {
            qrCodeGenerated.setTypeName("智能软件");
        } else if ("da".equals(qrCodeGenerated.getType())) {
            qrCodeGenerated.setTypeName("日常");
        } else if ("cg".equals(qrCodeGenerated.getType())) {
            qrCodeGenerated.setTypeName("采购");
        }
        return qrCodeGenerated;
    }

    public List<QrCodeGeneratedVO> tolist(List<QrCodeGeneratedVO> list) {
        for (QrCodeGeneratedVO qrCodeGenerated : list) {
            if ("jc".equals(qrCodeGenerated.getType())) {
                qrCodeGenerated.setTypeName("基础");
            } else if ("znh".equals(qrCodeGenerated.getType())) {
                qrCodeGenerated.setTypeName("智能硬件");
            } else if ("zns".equals(qrCodeGenerated.getType())) {
                qrCodeGenerated.setTypeName("智能软件");
            } else if ("da".equals(qrCodeGenerated.getType())) {
                qrCodeGenerated.setTypeName("日常");
            } else if ("cg".equals(qrCodeGenerated.getType())) {
                qrCodeGenerated.setTypeName("采购");
            }
        }
        return list;
    }

    public PageInfo<QrCodeInfo> selectListByQrCodeGeneratedId(int pageNum, int pageSize, Integer qrCodeGeneratedId) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(this.qrCodeInfoMapper.selectListByQrCodeGeneratedId(qrCodeGeneratedId, 1));
    }

    @Transactional(rollbackFor = {Exception.class})
    public Integer qrCodeDistribution(QrCodeDistributionDTO qrCodeDistribution) {
        try {
            int count = 0;
            for (String str : qrCodeDistribution.getIds().split(",")) {
                this.qrCodeInfoMapper.updateQrCodeGeneratedId(qrCodeDistribution.getQrCodeGeneratedId(), Integer.valueOf(Integer.parseInt(str)));
                count++;
            }
            return Integer.valueOf(count);
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return 0;
        }
    }

    @Transactional(rollbackFor = {Exception.class})
    public Integer appUpdateQrCodeGeneratedId(AppQrCodeDistributionDTO qrCodeDistribution) {
        try {
            int count = 0;
            for (String str : qrCodeDistribution.getQrCodes().split(",")) {
                this.qrCodeInfoMapper.appUpdateQrCodeGeneratedId(qrCodeDistribution.getQrCodeGeneratedId(), str);
                count++;
            }
            return Integer.valueOf(count);
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return 0;
        }
    }

    @Transactional(rollbackFor = {Exception.class})
    public Integer updateQrCodeGeneratedIdIsNull(QrCodeDistributionDTO qrCodeDistribution) {
        try {
            int count = 0;
            for (String str : qrCodeDistribution.getIds().split(",")) {
                this.qrCodeInfoMapper.updateQrCodeGeneratedIdIsNull(qrCodeDistribution.getQrCodeGeneratedId(), Integer.valueOf(Integer.parseInt(str)));
                count++;
            }
            return Integer.valueOf(count);
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return 0;
        }
    }

    public List<QrCodeInfo> undistributedQrCode(Integer counter, String qrCode) {
        return this.qrCodeInfoMapper.undistributedQrCode(counter, qrCode);
    }

    public List<QrCodeInfoImport> findPrintQrCode(Integer qrCodeGeneratedId, Integer type) {
        return this.qrCodeInfoMapper.findPrintQrCode(qrCodeGeneratedId, type);
    }

    public Integer undistributedSelect() {
        return this.qrCodeInfoMapper.undistributedSelect();
    }

    public Integer selectCount(String type, String buildNum, Integer sectionId) {
        return this.qrCodeGeneratedMapper.selectCount(type, buildNum, sectionId);
    }

    public Integer checkQrCode(String qrCode) {
        return this.qrCodeInfoMapper.checkQrCode(qrCode);
    }
}